{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "authorship_tag": "ABX9TyP6CwnnFqQxPYIejVMT+QMF",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/LC1332/personality-text-generation/blob/main/lulu_exp/%E5%B0%86%E5%88%86%E6%95%B0%E6%89%93%E5%8D%B0%E4%B8%BA%E8%A1%A8%E6%A0%BC%E6%88%96%E8%80%85%E5%8F%AF%E8%A7%86%E5%8C%96.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Bbuu04yJUsYU",
        "outputId": "f5292351-e12d-4619-c415-0c47e3b76930"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Mounted at /content/drive\n"
          ]
        }
      ],
      "source": [
        "from google.colab import drive\n",
        "drive.mount('/content/drive')"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "fname_response = '/content/drive/MyDrive/Psy/psy_score_openai.jsonl'"
      ],
      "metadata": {
        "id": "STC1WiiaU247"
      },
      "execution_count": 2,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import json\n",
        "\n",
        "datas = []\n",
        "\n",
        "with open(fname_response, encoding='utf-8') as f:\n",
        "    for line in f:\n",
        "        data = json.loads(line)\n",
        "        datas.append(data)"
      ],
      "metadata": {
        "id": "ESw3J9q1VAZD"
      },
      "execution_count": 3,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [],
      "metadata": {
        "id": "3kCVhAKcc5Iq"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "print(len(datas))\n",
        "print(datas[0])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "NTpkaSghVD23",
        "outputId": "8af78c7e-3070-4567-ec01-99956e9fd34a"
      },
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "32\n",
            "{'role_name': '汤师爷', 'N': {'score': 0.4, 'sub_factors': {'N4': 0.4, 'N1': -5.0, 'N3': 0.4, 'N2': 5.0, 'N6': 1.75}}, 'C': {'score': 0.0, 'sub_factors': {'C4': 1.0, 'C2': -0.2857142857142857, 'C1': 1.0, 'C5': -1.0, 'C3': -0.6666666666666666, 'C6': 0.0}}, 'A': {'score': -0.25, 'sub_factors': {'A2': -0.3333333333333333, 'A4': -0.3333333333333333, 'A1': -0.3333333333333333, 'A3': 0.0, 'A5': 0.0}}, 'O': {'score': -0.2857142857142857, 'sub_factors': {'O5': -0.4, 'O3': 0.0, 'O1': 2.0, 'O2': -0.4, 'O4': 0.0, 'O6': -1.0}}, 'E': {'score': -2.0, 'sub_factors': {'E6': -2.0, 'E2': -2.0, 'E4': -2.0, 'E1': -2.0, 'E3': -2.0}}, 'qids': [[20, 75, 77, 69], [25, 40, 50, 70], [30, 45, 55], [35, 72, 73], [71, 76, 74], [1, 78, 36, 81], [41, 21, 80, 85], [82, 79, 84], [6, 83, 46], [11, 26, 51], [17, 31, 56], [2, 62, 47], [7, 27, 65], [12, 32, 66], [16, 63, 52], [60, 64, 57], [61, 37, 67], [22, 42, 68], [3, 43, 38], [8, 88, 48], [33, 90, 58], [86, 94, 93], [87, 18, 95], [92, 28, 97], [13, 89, 53], [23, 91, 96], [4, 99, 106], [24, 100, 107], [49, 105, 54], [98, 29, 101], [9, 34, 104], [14, 103, 108], [19, 44, 59], [39, 102, 109]], 'N_table': {'N4': [-5, 5, 2, -2, 2], 'N1': [-5, 'null', 'null', 'null', 'null'], 'N3': [-5, 5, 2, -2, 2], 'N2': ['null', 5, 'null', 'null', 'null'], 'N6': ['null', 5, 2, -2, 2]}, 'C_table': {'C4': [2, 2, 'null', 'null', 'null', 'null', 2, -2], 'C2': [2, 2, -2, 'null', -2, -2, 2, -2], 'C1': [2, 2, -2, 2, 'null', 'null', 'null', 'null'], 'C5': ['null', 'null', -2, 2, -2, -2, 'null', 'null'], 'C3': ['null', 'null', 'null', 2, -2, -2, 'null', 'null'], 'C6': ['null', 'null', 'null', 'null', 'null', 'null', 2, -2]}, 'A_table': {'A2': [2, 0, -2, -2, 'null', 'null', 2, -2], 'A4': [2, 0, -2, -2, -2, 2, 'null', 'null'], 'A1': [2, 0, -2, -2, -2, 2, 'null', 'null'], 'A3': ['null', 'null', 'null', 'null', -2, 2, 2, -2], 'A5': ['null', 'null', 'null', 'null', 'null', 'null', 2, -2]}, 'O_table': {'O5': [2, 2, -2, 'null', 'null', -2, -2], 'O3': [2, 'null', 'null', 'null', 'null', 'null', -2], 'O1': [2, 'null', 'null', 'null', 'null', 'null', 'null'], 'O2': ['null', 2, -2, 2, -2, -2, 'null'], 'O4': ['null', 2, -2, 2, -2, 'null', 'null'], 'O6': ['null', 'null', 'null', 2, -2, -2, -2]}, 'E_table': {'E6': [-2, -2, 'null', -2, -2, -2], 'E2': [-2, 'null', 'null', -2, -2, -2], 'E4': [-2, -2, -2, 'null', -2, -2], 'E1': [-2, -2, -2, 'null', 'null', 'null'], 'E3': ['null', -2, -2, -2, 'null', 'null']}}\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "long_name = {\n",
        "    'N':'neuroticism',\n",
        "    'E':'extraversion',\n",
        "    'O':'openness',\n",
        "    'A':'agreeableness',\n",
        "    'C':'conscientiousness'\n",
        "}"
      ],
      "metadata": {
        "id": "DmKWS8h8VFUi"
      },
      "execution_count": 5,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "print('角色', end = ' | ')\n",
        "\n",
        "for key in long_name.keys():\n",
        "    print(long_name[key], end = ' | ')\n",
        "\n",
        "print()\n",
        "\n",
        "print('|-|',end = '')\n",
        "\n",
        "for key in long_name.keys():\n",
        "    print('-', end = ' | ')\n",
        "\n",
        "print()\n",
        "\n",
        "\n",
        "for data in datas:\n",
        "    role_name = data['role_name']\n",
        "\n",
        "    print('|', role_name, end = ' | ')\n",
        "\n",
        "    for key in long_name.keys():\n",
        "        major_score = data[key]['score']\n",
        "\n",
        "        print('%.3f' % major_score, end = ' | ')\n",
        "\n",
        "    print()\n",
        "\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "2TAjWaK-VLuC",
        "outputId": "3facdac7-2575-44c9-c839-eab352ad9fee"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "角色 | neuroticism | extraversion | openness | agreeableness | conscientiousness | \n",
            "|-|- | - | - | - | - | \n",
            "| 汤师爷 | 0.400 | -2.000 | -0.286 | -0.250 | 0.000 | \n",
            "| 慕容复 | -3.800 | 1.167 | 1.286 | -0.750 | 3.375 | \n",
            "| 李云龙 | -2.000 | -0.667 | -2.857 | -1.000 | 1.500 | \n",
            "| Luna | -0.400 | 2.000 | 5.000 | 2.250 | 0.125 | \n",
            "| 王多鱼 | -2.600 | 3.500 | 0.714 | -1.500 | -1.000 | \n",
            "| Ron | -0.600 | -0.833 | -1.429 | 1.125 | -0.750 | \n",
            "| 鸠摩智 | -2.000 | -2.000 | 1.333 | -0.250 | 2.500 | \n",
            "| Snape | -1.200 | -2.500 | -2.000 | -2.000 | 1.750 | \n",
            "| 凉宫春日 | -2.600 | 2.833 | 3.714 | -1.500 | 1.500 | \n",
            "| Malfoy | -2.000 | -2.000 | -2.000 | -2.000 | 1.750 | \n",
            "| 虚竹 | 1.200 | -2.500 | 0.714 | 4.250 | nan | \n",
            "| 萧峰 | -2.000 | -0.167 | -0.857 | 1.500 | 3.375 | \n",
            "| 段誉 | -1.800 | 2.167 | 1.714 | 4.625 | 0.875 | \n",
            "| Hermione | -2.000 | 0.000 | 4.571 | 2.000 | 4.250 | \n",
            "| Dumbledore | -2.000 | nan | 4.571 | 3.125 | 2.625 | \n",
            "| 王语嫣 | 1.000 | -0.333 | 2.857 | 3.000 | 2.750 | \n",
            "| Harry | -0.400 | nan | -0.286 | 2.500 | 3.750 | \n",
            "| McGonagall | -1.600 | -1.667 | 2.000 | 1.875 | 3.500 | \n",
            "| 白展堂 | -1.600 | 1.667 | 1.714 | 0.875 | 0.750 | \n",
            "| 佟湘玉 | -3.200 | 1.167 | 1.143 | 2.875 | 1.375 | \n",
            "| 郭芙蓉 | -2.000 | 4.000 | nan | 0.125 | -1.000 | \n",
            "| 旅行者 | -2.600 | -1.333 | 0.857 | -1.500 | -1.500 | \n",
            "| 钟离 | -3.000 | -2.000 | 1.286 | 1.000 | 2.875 | \n",
            "| 胡桃 | -2.600 | 5.000 | 4.571 | 1.875 | -0.500 | \n",
            "| Sheldon | -2.600 | -1.667 | 1.571 | nan | 4.625 | \n",
            "| Raj | 2.600 | 1.167 | 3.714 | 1.500 | 1.750 | \n",
            "| Penny | -1.200 | 2.833 | -0.286 | 3.500 | -1.250 | \n",
            "| 韦小宝 | -3.800 | 3.500 | -0.857 | -1.000 | -0.750 | \n",
            "| 乔峰 | -2.600 | 0.500 | -0.857 | 0.375 | 3.625 | \n",
            "| 神里绫华 | -1.200 | 0.333 | 2.857 | 3.500 | 4.625 | \n",
            "| 雷电将军 | -3.200 | -2.500 | -2.000 | 1.000 | 4.625 | \n",
            "| 于谦 | 1.000 | -0.667 | -0.857 | -1.000 | -2.750 | \n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "long_name = {\n",
        "    'N':'neuroticism',\n",
        "    'E':'extraversion',\n",
        "    'O':'openness',\n",
        "    'A':'agreeableness',\n",
        "    'C':'conscientiousness'\n",
        "}\n",
        "\n",
        "datas是一个list of json\n",
        "\n",
        "其中每个元素一定包含role_name字段\n",
        "\n",
        "检查datas的每一个元素，是否包含N_table, E_table等对应的table字段\n",
        "\n",
        "如果没有，则打印 <role_name>不包含<X_table>\n",
        "\n",
        "用python为我实现"
      ],
      "metadata": {
        "id": "kxE1IeJLc-03"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "long_name = {\n",
        "    'N':'neuroticism',\n",
        "    'E':'extraversion',\n",
        "    'O':'openness',\n",
        "    'A':'agreeableness',\n",
        "    'C':'conscientiousness'\n",
        "}\n",
        "\n",
        "for data in datas:\n",
        "    role_name = data['role_name']\n",
        "    for short_name, long_factor in long_name.items():\n",
        "        table_name = f'{short_name}_table'\n",
        "        if table_name not in data:\n",
        "            print(f'{role_name}不包含{table_name}')"
      ],
      "metadata": {
        "id": "nOtvUicXVUgR"
      },
      "execution_count": 6,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "{\"role_name\": \"Dumbledore\", \"N\": {\"score\": -2.0, \"sub_factors\": {\"N4\": -2.0, \"N1\": -2.0, \"N3\": -2.0, \"N2\": -2.0, \"N6\": -2.0}}, \"C\": {\"score\": 2.625, \"sub_factors\": {\"C4\": 3.5, \"C2\": 2.7142857142857144, \"C1\": 3.5, \"C5\": 1.75, \"C3\": 0.6666666666666666, \"C6\": 3.5}}, \"A\": {\"score\": 3.125, \"sub_factors\": {\"A2\": 3.5, \"A4\": 3.5, \"A1\": 3.5, \"A3\": 2.0, \"A5\": 2.0}}, \"O\": {\"score\": 4.571428571428571, \"sub_factors\": {\"O5\": 4.4, \"O3\": 3.5, \"O1\": 5.0, \"O2\": 5.0, \"O4\": 5.0, \"O6\": 4.25}}, \"E\": {\"score\": NaN, \"sub_factors\": {\"E4\": 2.6, \"E1\": 2.0}}, \"qids\": [[25, 40, 50, 70], [30, 45, 55], [35, 72, 73], [71, 76, 74], [1, 78, 36, 81], [41, 21, 80, 85], [82, 79, 84], [6, 83, 46], [11, 26, 51], [17, 31, 56], [2, 62, 47], [7, 27, 65], [12, 32, 66], [16, 63, 52], [60, 64, 57], [61, 37, 67], [22, 42, 68], [3, 43, 38], [8, 88, 48], [33, 90, 58], [86, 94, 93], [87, 18, 95], [92, 28, 97], [13, 89, 53], [23, 91, 96], [4, 99, 106], [24, 100, 107], [49, 105, 54], [98, 29, 101], [9, 34, 104], [14, 103, 108], [19, 44, 59], [39, 102, 109]], \"N_table\": {\"N4\": [-2, -2, -2, -2, -2], \"N1\": [-2, \"null\", \"null\", \"null\", \"null\"], \"N3\": [-2, -2, -2, -2, -2], \"N2\": [\"null\", -2, \"null\", \"null\", \"null\"], \"N6\": [\"null\", -2, -2, -2, -2]}, \"C_table\": {\"C4\": [2, 5, \"null\", \"null\", \"null\", \"null\", 2, 5], \"C2\": [2, 5, 5, \"null\", -2, 2, 2, 5], \"C1\": [2, 5, 5, 2, \"null\", \"null\", \"null\", \"null\"], \"C5\": [\"null\", \"null\", 5, 2, -2, 2, \"null\", \"null\"], \"C3\": [\"null\", \"null\", \"null\", 2, -2, 2, \"null\", \"null\"], \"C6\": [\"null\", \"null\", \"null\", \"null\", \"null\", \"null\", 2, 5]}, \"A_table\": {\"A2\": [5, 5, 2, 5, \"null\", \"null\", 2, 2], \"A4\": [5, 5, 2, 5, 2, 2, \"null\", \"null\"], \"A1\": [5, 5, 2, 5, 2, 2, \"null\", \"null\"], \"A3\": [\"null\", \"null\", \"null\", \"null\", 2, 2, 2, 2], \"A5\": [\"null\", \"null\", \"null\", \"null\", \"null\", \"null\", 2, 2]}, \"O_table\": {\"O5\": [5, 5, 5, \"null\", \"null\", 5, 2], \"O3\": [5, \"null\", \"null\", \"null\", \"null\", \"null\", 2], \"O1\": [5, \"null\", \"null\", \"null\", \"null\", \"null\", \"null\"], \"O2\": [\"null\", 5, 5, 5, 5, 5, \"null\"], \"O4\": [\"null\", 5, 5, 5, 5, \"null\", \"null\"], \"O6\": [\"null\", \"null\", \"null\", 5, 5, 5, 2]}, \"E_table\": {\"E6\": [2, 2, \"null\", \"2\", 2, 5], \"E2\": [2, \"null\", \"null\", \"2\", 2, 5], \"E4\": [2, 2, 2, \"null\", 2, 5], \"E1\": [2, 2, 2, \"null\", \"null\", \"null\"], \"E3\": [\"null\", 2, 2, \"2\", \"null\", \"null\"]}}\n"
      ],
      "metadata": {
        "id": "OVzkD5lTdfp8"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "recount_score = {}\n",
        "\n",
        "\n",
        "\n",
        "for data in datas:\n",
        "    role_name = data['role_name']\n",
        "\n",
        "    for short_name in long_name.keys():\n",
        "        n = -1\n",
        "        res = []\n",
        "        table_name = f'{short_name}_table'\n",
        "        for k in data[table_name].keys():\n",
        "            v = data[table_name][k]\n",
        "            if n == -1:\n",
        "                n = len(v)\n",
        "                res = [-999 for _ in range(n)]\n",
        "            for i, a in enumerate(v):\n",
        "                if a!='null' and res[i]==-999:\n",
        "                    res[i] = a\n",
        "\n",
        "            for i in range(len(res)):\n",
        "                if isinstance(res[i], str):\n",
        "                    res[i] = int(res[i])\n",
        "\n",
        "        # n = len()\n",
        "        average = float(sum(res))/ float(len(res))\n",
        "        recount_score[(role_name, short_name)] = average\n",
        "        print(res)\n",
        "        break\n",
        "    break"
      ],
      "metadata": {
        "id": "dfApKiHGdvGP",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "8fc953af-51c5-45e2-ae6d-ece327e6b394"
      },
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[-5, 5, 2, -2, 2]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(role_name)\n",
        "print(long_name[short_name])\n",
        "print(res)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "v5fHLCkNJdRA",
        "outputId": "7dbb9da4-cf2b-4067-89df-7a2e05a935aa"
      },
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "汤师爷\n",
            "neuroticism\n",
            "[-5, 5, 2, -2, 2]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print('角色', end = ' | ')\n",
        "\n",
        "for key in long_name.keys():\n",
        "    print(long_name[key], end = ' | ')\n",
        "\n",
        "print()\n",
        "\n",
        "print('|-|',end = '')\n",
        "\n",
        "for key in long_name.keys():\n",
        "    print('-', end = ' | ')\n",
        "\n",
        "print()\n",
        "\n",
        "\n",
        "for data in datas:\n",
        "    role_name = data['role_name']\n",
        "\n",
        "    print('|', role_name, end = ' | ')\n",
        "\n",
        "    for key in long_name.keys():\n",
        "        # major_score = data[key]['score']\n",
        "        major_score = recount_score[(role_name, key)]\n",
        "\n",
        "        print('%.3f' % major_score, end = ' | ')\n",
        "\n",
        "    print()\n",
        "\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "6cfefZ3DdU2N",
        "outputId": "373700c4-0124-419c-aba8-54eaa7375097"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "角色 | neuroticism | extraversion | openness | agreeableness | conscientiousness | \n",
            "|-|- | - | - | - | - | \n",
            "| 汤师爷 | 0.400 | -2.000 | -0.286 | -0.250 | 0.000 | \n",
            "| 慕容复 | -3.800 | 1.167 | 1.286 | -0.750 | 3.375 | \n",
            "| 李云龙 | -2.000 | -0.667 | -2.857 | -1.000 | 1.500 | \n",
            "| Luna | -0.400 | 2.000 | 5.000 | 2.250 | 0.125 | \n",
            "| 王多鱼 | -2.600 | 3.500 | 0.714 | -1.500 | -1.000 | \n",
            "| Ron | -0.600 | -0.833 | -1.429 | 1.125 | -0.750 | \n",
            "| 鸠摩智 | -2.000 | -2.000 | 1.333 | -0.250 | 2.500 | \n",
            "| Snape | -1.200 | -2.500 | -2.000 | -2.000 | 1.750 | \n",
            "| 凉宫春日 | -2.600 | 2.833 | 3.714 | -1.500 | 1.500 | \n",
            "| Malfoy | -2.000 | -2.000 | -2.000 | -2.000 | 1.750 | \n",
            "| 虚竹 | 1.200 | -2.500 | 0.714 | 4.250 | 0.875 | \n",
            "| 萧峰 | -2.000 | -0.167 | -0.857 | 1.500 | 3.375 | \n",
            "| 段誉 | -1.800 | 2.167 | 1.714 | 4.625 | 0.875 | \n",
            "| Hermione | -2.000 | 0.000 | 4.571 | 2.000 | 4.250 | \n",
            "| Dumbledore | -2.000 | 2.500 | 4.571 | 3.125 | 2.625 | \n",
            "| 王语嫣 | 1.000 | -0.333 | 2.857 | 3.000 | 2.750 | \n",
            "| Harry | -0.400 | -0.500 | -0.286 | 2.500 | 3.750 | \n",
            "| McGonagall | -1.600 | -1.667 | 2.000 | 1.875 | 3.500 | \n",
            "| 白展堂 | -1.600 | 1.667 | 1.714 | 0.875 | 0.750 | \n",
            "| 佟湘玉 | -3.200 | 1.167 | 1.143 | 2.875 | 1.375 | \n",
            "| 郭芙蓉 | -2.000 | 4.000 | -1.857 | 0.125 | -1.000 | \n",
            "| 旅行者 | -2.600 | -1.333 | 0.857 | -1.500 | -1.500 | \n",
            "| 钟离 | -3.000 | -2.000 | 1.286 | 1.000 | 2.875 | \n",
            "| 胡桃 | -2.600 | 5.000 | 4.571 | 1.875 | -0.500 | \n",
            "| Sheldon | -2.600 | -1.667 | 1.571 | -1.500 | 4.625 | \n",
            "| Raj | 2.600 | 1.167 | 3.714 | 1.500 | 1.750 | \n",
            "| Penny | -1.200 | 2.833 | -0.286 | 3.500 | -1.250 | \n",
            "| 韦小宝 | -3.800 | 3.500 | -0.857 | -1.000 | -0.750 | \n",
            "| 乔峰 | -2.600 | 0.500 | -0.857 | 0.375 | 3.625 | \n",
            "| 神里绫华 | -1.200 | 0.333 | 2.857 | 3.500 | 4.625 | \n",
            "| 雷电将军 | -3.200 | -2.500 | -2.000 | 1.000 | 4.625 | \n",
            "| 于谦 | 1.000 | -0.667 | -0.857 | -1.000 | -2.750 | \n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "fname_eval = '/content/drive/MyDrive/Psy/psy_eval_gpt.txt'"
      ],
      "metadata": {
        "id": "IYIVDlPueB1G"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import json\n",
        "\n",
        "datas = []\n",
        "\n",
        "with open(fname_eval, encoding='utf-8') as f:\n",
        "    for line in f:\n",
        "        data = json.loads(line)\n",
        "        datas.append(data)"
      ],
      "metadata": {
        "id": "swmm26ik4ZmD"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "save_name = '/content/drive/MyDrive/Psy/psy_eval_gpt_with_id.txt'"
      ],
      "metadata": {
        "id": "TuhTKDaR4cxb"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "eval_id = 0\n",
        "with open(save_name, 'w', encoding='utf-8') as f:\n",
        "    for data in datas:\n",
        "        data['eval_batch_id'] = eval_id\n",
        "        json.dump(data, f, ensure_ascii=False)\n",
        "        f.write('\\n')\n",
        "        eval_id += 1\n",
        "\n"
      ],
      "metadata": {
        "id": "45izX-Bo4pr1"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "fname_eval = '/content/drive/MyDrive/Psy/psy_eval_gpt.txt'\n",
        "\n",
        "import json\n",
        "\n",
        "datas = []\n",
        "\n",
        "with open(fname_eval, encoding='utf-8') as f:\n",
        "    for line in f:\n",
        "        data = json.loads(line)\n",
        "        datas.append(data)\n",
        "\n",
        "save_name = '/content/drive/MyDrive/Psy/psy_eval_gpt_with_id.txt'\n",
        "\n",
        "import json\n",
        "\n",
        "\n",
        "with open(save_name, 'w', encoding='utf-8') as f:\n",
        "\n",
        "\n",
        "我现在想在每条data中增加一个字段 eval_batch_id 从0开始计数\n",
        "\n",
        "保存在save_name, ensure_assic = False\n",
        "\n",
        "请用python为我实现"
      ],
      "metadata": {
        "id": "pVT5BU0F4wRl"
      }
    }
  ]
}